package com.quick.android.handler.http;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;

import com.quick.android.handler.http.impl.OnResponseListener;

public class HttpClientResponse {
	private String mUrl;
	private String mContent;
	private File mDownloadFile;

	public HttpClientResponse(String url) {
		mUrl = url;
	}

	public String getUrl() {
		return mUrl;
	}

	public void setUrl(String url) {
		this.mUrl = url;
	}

	public String getContent() {
		return mContent;
	}

	public void setContent(String content) {
		this.mContent = content;
	}

	public File getDownloadFile() {
		return mDownloadFile;
	}

	public void setDownloadFile(File downloadFile) {
		this.mDownloadFile = downloadFile;
	}

	public void writeFileData(String path, HttpEntity entity,
			OnResponseListener<String> listener) throws IllegalStateException,
			IOException {
		// 设置本地保存的文件
		mDownloadFile = new File(path);
		FileOutputStream output = new FileOutputStream(mDownloadFile);
		long allSize = entity.getContentLength();
		long readSize = 0l;
		// 得到网络资源并写入文件
		InputStream input = entity.getContent();
		byte b[] = new byte[1024];
		int j = 0;
		while ((j = input.read(b)) != -1) {
			output.write(b, 0, j);
			readSize += j;
			if (listener != null) {
				listener.onProgress(readSize * 100d / allSize);
			}
		}
		output.flush();
		output.close();
	}

}
